//
//  User.m
//  PDA
//
//  Created by 吕晶 on 2020/1/8.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "User.h"
#import "LoginVC.h"
#import "NewLoginVC.h"

static User *shareUser;
static dispatch_once_t once;

@implementation User

+ (User *)shareSingleton {
    dispatch_once(&once, ^ {
        shareUser = [[User alloc] init];
    });
    return shareUser;
}

+ (void)userRelease {
    once = 0;
    shareUser = nil;
}

//登录成功
+ (void)loginSuccess:(NSString *)token {
    if (![FactoryMethod isNull:token]) {
        [USER_D setObject:token forKey:@"User_token"];
        [USER_D synchronize];
        NSLog(@"token==%@",token);
        MainTabBarController *mainVC = [[MainTabBarController alloc] init];
        MAIN_WINDOW.rootViewController = mainVC;
    }
}

+ (void)loginSuccessData:(id)data {
    if (![FactoryMethod isNull:data[@"type"]]) {
        //1单车物流商 2多车物流商
        [USER_D setObject:data[@"type"] forKey:@"User_type"];
        [USER_D synchronize];
    }
    if (![FactoryMethod isNull:data[@"token"]]) {
        [USER_D setObject:data[@"token"] forKey:@"User_token"];
        [USER_D synchronize];
        NSLog(@"登录成功--token--%@",data[@"token"]);
        MainTabBarController *mainVC = [[MainTabBarController alloc] init];
        MAIN_WINDOW.rootViewController = mainVC;
        [[LJLocationManager shareManager] requestForAuth];
    }
    if (![FactoryMethod isNull:data[@"user_id"]]) {
        [USER_D setObject:data[@"user_id"] forKey:@"User_uid"];
        [USER_D synchronize];
        [JPUSHService setAlias:data[@"user_id"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            NSLog(@"别名设置成功--%@",data[@"user_id"]);
        } seq:1];
    }
//    if (![FactoryMethod isNull:data[@"group_id"]]) {
//        [USER_D setObject:data[@"group_id"] forKey:@"User_group_id"];
//        [USER_D synchronize];
//        [JPUSHService setTags:[NSSet setWithObject:data[@"group_id"]] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
//            NSLog(@"tag设置成功--%@",data[@"group_id"]);
//        } seq:1];
//    }
}

//登出成功
+ (void)logoutSuccess {
    if ([USER_D objectForKey:@"User_type"]) {
        [USER_D removeObjectForKey:@"User_type"];
    }
    if ([USER_D objectForKey:@"User_token"]) {
        [USER_D removeObjectForKey:@"User_token"];
        
//        if ([USER_D objectForKey:@"User_group_id"]) {
//            NSString *group_id = [USER_D objectForKey:@"User_group_id"];
//            [JPUSHService deleteTags:[NSSet setWithObject:group_id] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
//                
//            } seq:1];
//            [USER_D removeObjectForKey:@"User_group_id"];
//        }
        if ([USER_D objectForKey:@"User_uid"]) {
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                
            } seq:1];
            [USER_D removeObjectForKey:@"User_uid"];
        }
//        LoginVC *loginVC = [[LoginVC alloc] init];
        NewLoginVC *loginVC = [[NewLoginVC alloc] init];
        MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:loginVC];
        MAIN_WINDOW.rootViewController = nav;
    }
    [USER_D synchronize];
}
//判断用户是否登录
+ (BOOL)isLogin {
    if ([USER_D objectForKey:@"User_token"]) {
        NSLog(@"---%@",[USER_D objectForKey:@"User_token"]);
        return YES;
    }
    return NO;
}
//获取用户token
+ (NSString *)getUserToken {
    if ([USER_D objectForKey:@"User_token"]) {
        return [USER_D objectForKey:@"User_token"];
    }
    return nil;
}

//获取用户id
+ (NSString *)getUserID {
    if ([USER_D objectForKey:@"User_uid"]) {
        return [USER_D objectForKey:@"User_uid"];
    }
    return nil;
}

//获取用户角色 1-单车 2-多车
+ (NSString *)getUserType {
    if ([USER_D objectForKey:@"User_type"]) {
        return [USER_D objectForKey:@"User_type"];
    }
    return nil;
}

//判断用户是否为单车物流商
+ (BOOL)userIsDriver {
    if ([[USER_D objectForKey:@"User_type"] isEqualToString:@"1"]) {
        return YES;
    }
    return NO;
}

@end
